<template>
  <el-main>
    <!-- 搜索栏 -->
    <el-form :model="listParm" :inline="true" size="small">
      <el-form-item>
        <el-input
          placeholder="请输入读者证号"
          v-model="listParm.username"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="请输入姓名"
          v-model="listParm.learnNum"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="请输入电话号码"
          v-model="listParm.phone"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          placeholder="请输入身份证号码"
          v-model="listParm.idCard"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" round icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button type="danger" round icon="el-icon-close" @click="resetBtn">重置</el-button>
        <el-button v-permission="['sys:reader:add']" type="success" round icon="el-icon-plus" @click="addBtn">新增
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :height="tableHeight" :data="tableData" :default-sort="{prop:'tableData','order':'descending',}" border
              stripe
    >
      <el-table-column prop="learnNum" label="姓名"></el-table-column>
      <el-table-column prop="username" label="读者证号"></el-table-column>
      <el-table-column prop="className" label="班级" show-overflow-tooltip></el-table-column>
      <el-table-column prop="idCard" label="身份证号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="phone" label="电话" show-overflow-tooltip></el-table-column>
      <el-table-column prop="sex" label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex == '0'">男</el-tag>
          <el-tag v-if="scope.row.sex == '1'" type="success">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.checkStatus == '0'" type="danger">未审核</el-tag>
          <el-tag v-if="scope.row.checkStatus == '1'" type="success">已审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userStatus == '0'" type="danger">停用</el-tag>
          <el-tag v-if="scope.row.userStatus == '1'" type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="停用/启用">
        <template slot-scope="scope">
          <!--绑定数值用:active-value 绑定字符串不用 一定注意-->
          <el-switch
            v-model="scope.row.userStatus"
            active-value="1"
            inactive-value="0"
            @change="switchChange(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="370px">
        <template slot-scope="scope">
          <el-button
            v-permission="['sys:reader:edit']"
            icon="el-icon-edit"
            type="primary"
            size="small"
            @click="editBtn(scope.row)"
          >编辑
          </el-button>
          <el-button
            v-permission="['sys:reader:examine']"
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="examineBtn(scope.row)"
          >审核
          </el-button>
          <el-button
            v-permission="['sys:reader:reset']"
            icon="el-icon-refresh-left"
            type="warning"
            size="small"
            @click="resetPassWordBtn(scope.row)"
          >重置密码
          </el-button>
          <el-button
            v-permission="['sys:reader:delete']"
            icon="el-icon-delete"
            type="danger"
            size="small"
            @click="deleteBtn(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :current-page.sync="listParm.currentPage"
      :page-sizes="[10, 20, 40, 80, 100]"
      :page-size="listParm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listParm.total"
      background
    >
    </el-pagination>
    <!-- 新增弹框 -->
    <sys-dialog
      :title="dialog.title"
      :width="dialog.width"
      :height="dialog.height"
      :visible="dialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm"
    >
      <div slot="content">
        <el-form
          :model="addModel"
          ref="addRef"
          :rules="rules"
          label-width="80px"
          size="small"
          style="margin-right: 30px"
        >
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="learnNum" label="姓名">
                <el-input v-model="addModel.learnNum"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="phone" label="电话">
                <el-input v-model="addModel.phone"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="username" label="读者证号">
                <el-input v-model="addModel.username"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12" :offset="0">
              <el-form-item prop="className" label="班级">
                <el-input v-model="addModel.className"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="idCard" label="身份证">
                <el-input v-model="addModel.idCard"></el-input>
              </el-form-item>
            </el-col>
            <el-col v-if="addModel.type==0" :span="12" :offset="0">
              <el-form-item prop="password" label="密码">
                <el-input v-model="addModel.password" show-password></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="0">
              <el-form-item prop="sex" label="性别">
                <el-radio-group v-model="addModel.sex">
                  <el-radio :label="'0'">男</el-radio>
                  <el-radio :label="'1'">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </sys-dialog>
  </el-main>
</template>
<script>
import SysDialog from '@/components/dialog/SysDialog.vue'
import {
  getListApi,
  addReaderApi,
  editReaderApi,
  deleteReaderApi,
  examineReaderApi,
  resetPassWordApi,
  updateStatusApi
} from '@/api/reader'
import { validateIdCard, validatePhone } from '@/utils/validate'

export default {
  //注册组件
  components: {
    SysDialog
  },
  data () {
    return {
      //弹框属性
      dialog: {
        title: '',
        width: 640,
        height: 230,
        visible: false
      },
      rules: {
        learnNum: [{ required: true, message: '请填写姓名', trigger: 'blur' }],
        username: [{ required: true, message: '请填写读者证号', trigger: 'blur' }],
        idCard: [{ required: true, message: '请填写身份证号', trigger: 'blur' }, {
          validator: validateIdCard, trigger: 'blur'
        }],
        phone: [{ required: true, message: '请填写电话', trigger: 'blur' }, {
          validator: validatePhone, trigger: 'blur'
        }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        password: [{ required: true, message: '请填写密码', trigger: 'blur' }],
        className: [{ required: true, message: '请填写班级', trigger: 'blur' }]
      },
      //表单属性
      addModel: {
        readerId: '',
        learnNum: '',
        username: '',
        idCard: '',
        sex: '',
        phone: '',
        password: '',
        type: '',
        className: '',
        userStatus:''
      },
      //表格高度
      tableHeight: 0,
      //表格数据
      tableData: [],
      //列表参数
      listParm: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
        username: '',
        idCard: '',
        phone: '',
        learnNum: ''
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 207
    })
  },
  created () {
    this.getList()
  },
  methods: {
    //弹框确定
    onConfirm () {
      this.$refs.addRef.validate(async(valid) => {
        if (valid) {
          this.dialog.visible = false
          let res = null
          if (this.addModel.type == '0') {
            res = await addReaderApi(this.addModel)
          } else {
            res = await editReaderApi(this.addModel)
          }
          if (res && res.code == 200) {
            //信息提示
            this.$message({ type: 'success', message: res.msg })
            //刷新表格
            this.getList()
          }
        }
      })
    },
    //Switch 开关
    async switchChange (row) {
      console.log(row)
      let res = await updateStatusApi({readerId: row.readerId} )
      console.log(res)
      if (res && res.code == 200) {
        //信息提示
        this.$message({ type: 'success', message: res.msg })
        //刷新表格
        this.getList()
      }
    },
    //弹框关闭
    onClose () {
      this.dialog.visible = false
    },
    //页数改变时触发
    currentChange (val) {
      this.listParm.currentPage = val
      this.getList()
    },
    //页容量改变时触发
    sizeChange (val) {
      this.listParm.pageSize = val
      this.getList()
    },
    //获取列表
    async getList () {
      let res = await getListApi(this.listParm)
      if (res && res.code == 200) {
        this.tableData = res.data.records
        this.listParm.total = res.data.total
      }
    },
    //删除按钮
    async deleteBtn (row) {
      let confirm = await this.$myconfirm('确定删除该数据吗?')
      if (confirm) {
        let res = await deleteReaderApi({ readerId: row.readerId })
        if (res && res.code == 200) {
          //信息提示
          this.$message({ type: 'success', message: res.msg })
          //刷新表格
          this.getList()
        }
      }
    },
    //审核按钮
    async examineBtn (row) {
      let confirm = await this.$myconfirm('确定审核吗?')
      if (confirm) {
        let res = await examineReaderApi({ readerId: row.readerId })
        if (res && res.code == 200) {
          this.$message({ type: 'success', message: res.msg })
          //刷新表格
          this.getList()
        }
      }
    },
    async resetPassWordBtn (row) {
      let confirm = await this.$myconfirm('确定重置密码吗?')
      if (confirm) {
        let res = await resetPassWordApi({ readerId: row.readerId })
        if (res && res.code == 200) {
          this.$message({ type: 'success', message: res.msg })
          this.$store.dispatch(logout)
        }
      }
    },
    //编辑按钮
    editBtn (row) {
      this.dialog.title = '编辑读者'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //把要编辑的数据复制到表单数据域
      this.$objCoppy(row, this.addModel)
      //设置编辑属性
      this.addModel.type = '1'
    },
    //新增按钮
    addBtn () {
      this.dialog.title = '新增读者'
      this.dialog.visible = true
      //清空表单
      this.$resetForm('addRef', this.addModel)
      //设置编辑属性
      this.addModel.type = '0'
    },
    //重置按钮
    resetBtn () {
      this.listParm.username = ''
      this.listParm.idCard = ''
      this.listParm.learnNum = ''
      this.listParm.phone = ''
      this.getList()
    },
    //搜索按钮
    searchBtn () {
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
